package com.hp.mvc.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 前置处理
     * 返回 false 进行拦截，返回 true 放行
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从session取User对象
        String user  = (String) request.getSession().getAttribute("username");
        if(user == null){
            //没有登录，拦截，重定向到登录页面
            response.sendRedirect("/mvc/login");
            return false;
        }
        return true;
    }
}
